扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:阎宏博士 来源:天极网 2007年11月9日
关键字:
Public Class Factory2A Private Shared instance As Product Private Shared m As Mutex = New Mutex() Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) m.WaitOne() If (instance Is Nothing) Then instance = New Product() End If m.ReleaseMutex() Return instance End Function End Class |
代码清单10、二重检查的线程安全的Singleton类 |
Public Class Factory2B Private Shared instance As Product Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) Monitor.Enter(GetType(Factory2B)) If (instance Is Nothing) Then instance = New Product() End If Monitor.Exit(GetType(Factory2B)) Return instance End Function End Class |
代码清单11、二重检查的线程安全的Singleton类 |
Public Class Factory2C Private Shared instance As Product Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) SyncLock (GetType(Factory2C)) If (instance Is Nothing) Then instance = New Product() End If End SyncLock Return instance End Function End Class |
代码清单12、二重检查的线程安全的Singleton类 |
Public Class Factory3A Private Shared instance As Product Public Shared Function GetInstance() As Product Thread.Sleep(10) If (instance Is Nothing) Then Monitor.Enter(GetType(Factory3A)) If (instance Is Nothing) Then instance = New Product() End If Monitor.Exit(GetType(Factory3A)) End If Return instance End Function End Class |
代码清单13、二重检查的线程安全的Singleton类 |
Public Class Factory3B Private Shared instance As Product Public Shared Function GetInstance() As Product Thread.Sleep(10) If (instance Is Nothing) Then SyncLock (GetType(Factory3B)) If (instance Is Nothing) Then instance = New Product() End If End SyncLock End If Return instance End Function End Class |
代码清单14、二重检查的线程安全的Singleton类 |
Public Class SingletonA Private Shared instance As SingletonA Public Sub New() System.Console.WriteLine("Singleton object is created.") End Sub Public Shared Function GetInstance() As SingletonA Thread.Sleep(10) If instance Is Nothing Then Monitor.Enter(GetType(SingletonA)) If instance Is Nothing Then instance = New SingletonA() End If Monitor.Exit(GetType(SingletonA)) End If Return instance End Function End Class |
代码清单15、二重检查的线程安全的Singleton类 |
Public Class SingletonB Private Shared instance As SingletonB Public Sub New() System.Console.WriteLine("Singleton object is created.") End Sub Public Shared Function GetInstance() As SingletonB Thread.Sleep(10) If instance Is Nothing Then SyncLock (GetType(SingletonB)) If instance Is Nothing Then instance = New SingletonB() End If End SyncLock End If Return instance End Function End Class |
濠碘€冲€归悘澶愬箖閵娾晜濮滈悽顖涚摃閹烩晠宕氶崶鈺傜暠闁诡垰鍘栫花锛勬喆椤ゅ弧濡澘妫楅悡娆撳嫉閳ь剟寮0渚€鐛撻柛婵呮缁楀矂骞庨埀顒勫嫉椤栨瑤绻嗛柟顓у灲缁辨繈鏌囬敐鍕杽閻犱降鍨藉Σ鍕嚊閹跺鈧﹦绱旈幋鐐参楅柡鍫灦閸嬫牗绂掔捄铏规闁哄嫷鍨遍崑宥夋儍閸曨剚浠樺ù锝嗗▕閳ь剚鏌ㄧ欢鐐寸▕鐎b晝顏遍柕鍡嫹